/*
 * LitePlayer.cpp
 *
 *  Created on: 2016年3月16日
 *      Author: terry
 */

#include "LitePlayer.h"

#include "CLitePlayer.h"
#include "CLog.h"
#include "Path.h"

#include "AppProperties.h"
#include "TSafeStr.h"

#include "AVFilterPtr.h"
#include "TStringUtil.h"
#include "TFileUtil.h"

#include "SDL.h"


namespace av
{

static const char*	LOG_CONFIG_FILENAME = "LitePlayerLog.ini";
static const char*	CONFIG_FILENAME = "LitePlayer.ini";



std::string getLogConfigPath()
{
	std::string filePath = comn::Path::getWorkDir();
	filePath = comn::Path::join(filePath, LOG_CONFIG_FILENAME);
	return filePath;
}

std::string getConfigPath()
{
	std::string filePath = comn::Path::getWorkDir();
	filePath = comn::Path::join(filePath, CONFIG_FILENAME);
	return filePath;
}


void setupLog()
{
	std::string filePath = getLogConfigPath();
	int rc = CLog::reload(filePath.c_str());
	if (rc != 0)
	{
		CLog::setLogger(CLog::DEBUGWINDOW, CLog::kNone, CLog::TIME);
		CLog::setLogger(CLog::COUT, CLog::kNone, CLog::TIME);
	}

}


#ifdef WIN32
#define	FFMPEG_LOG_FILE		"ffmpeg.log"
#else
#define	FFMPEG_LOG_FILE		"/var/log/ffmpeg.log"
#endif //

void av_log_callback(void *avcl, int level, const char *fmt,
                     va_list vl)
{
    av_log_default_callback(avcl, level, fmt, vl);

    if (level <= AV_LOG_WARNING)
    {
        const size_t MAX_SIZE = 1024 * 2;
        char buffer[MAX_SIZE] = {0};

        char* buf = buffer;
        int len = comn::StringUtil::vsprint(&buf, MAX_SIZE, fmt, vl);

#ifdef WIN32
        OutputDebugString(buf);
#endif //

        comn::FileUtil::write(buf, len, FFMPEG_LOG_FILE, true);

        if (buf != buffer)
        {
            free(buf);
        }
    }
}


bool LitePlayerFactory::startup()
{
	av_register_all();
	avformat_network_init();
    avfilter_register_all();

	SDL_Init(SDL_INIT_EVERYTHING);

	setupLog();

	CLog::info("LitePlayerFactory::startup.\n");

	std::string configPath = getConfigPath();
	AppProperties::getProperties().load(configPath.c_str());

	return true;
}

void LitePlayerFactory::cleanup()
{
	avformat_network_deinit();

	SDL_Quit();

}

/**
 * 创建播放器
 * @param hwnd
 * @return NULL if failed
 */
LitePlayer* LitePlayerFactory::create(void* hwnd)
{
	CLitePlayer* player = new CLitePlayer();
	player->setVideoWnd(hwnd);
	return player;
}

/**
 * 销毁播放器
 * @param pPlayer
 */
void LitePlayerFactory::destroy(LitePlayer* pPlayer)
{
	if (!pPlayer)
	{
		return;
	}

	pPlayer->close();
	delete pPlayer;
}


size_t LitePlayerFactory::getProp(const char* name, char* value, size_t size)
{
	if ((!value) || (size == 0))
	{
		return -1;
	}

	std::string str;
	if (!AppProperties::getProperties().get(name, str))
	{
		return 0;
	}

	return comn::copyStr(value, size, str);
}

/**
 * 设置全局属性
 * @param name	属性名称
 * @param value	属性值
 * @return
 */
bool LitePlayerFactory::setProp(const char* name, const char* value)
{
	return AppProperties::getProperties().set(name, value);
}

/**
 * 是否存在属性
 * @param name
 * @return
 */
bool LitePlayerFactory::existProp(const char* name)
{
	return AppProperties::getProperties().exist(name);
}

void LitePlayerFactory::enableLog(const char* filename)
{
    if (!filename || strlen(filename) == 0)
    {
        CLog::resetLogger();
    }
    else
    {
        CLog::setFileParam(filename, 10, 2);

        CLog::setLogger(CLog::FILE, CLog::kDebug, 0);

        av_log_set_level(AV_LOG_DEBUG);
        av_log_set_callback(av_log_callback);
    }
}


InputPlayer* LitePlayerFactory::createInputPlayer(void* hwnd)
{
	CLitePlayer* player = new CLitePlayer();
	player->setVideoWnd(hwnd);
	return player;
}




} /* namespace av */
